﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Win32;

namespace Ramp.VSAddIn.ToolsOptions
{
    public class Settings
    {
        #region Private Fields
        
        private const string regRamp = @"SOFTWARE\Microsoft\VisualStudio\10.0\Ramp Service";
        
        #endregion Private Fields

        #region Constructors

        private Settings()
        { }

        static Settings()
        {
            ServiceHostName = "localhost";

            ServicePortNumber = 9898;
        }

        #endregion Constructors

        #region Properties

        public static string ServiceHostName;

        public static int ServicePortNumber;

        #endregion Properties

        #region Public Methods

        /// <summary>
        /// Loads options page settings from registry.
        /// </summary>
        public static void Load()
        {
            RegistryKey regKey = null;
            try
            {
                regKey = Registry.CurrentUser.OpenSubKey(regRamp, false);
                if (regKey != null)
                {
                    Settings.ServiceHostName = (string)regKey.GetValue("RampWindowsServiceHostName", "localhost");
                    Settings.ServicePortNumber = (int)regKey.GetValue("RampWindowsServicePortNumber", 9898);

                    //Debug.WriteLine("Ramper AddIn: Settings: Got RampWindowsServiceHostName as: " + Settings.ServiceHostName);
                    //Debug.WriteLine("Ramper AddIn: Settings: Got RampWindowsServicePortNumber as: " + Settings.ServicePortNumber);
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine("Ramper AddIn: Ramp Service failed to load: " + exception.Message);
            }
            finally
            {
                if (regKey != null)
                {
                    regKey.Close();
                }
            }
        }

        /// <summary>
        /// Saves options page settings to registry.
        /// </summary>
        public static void Save()
        {
            RegistryKey regKey = null;
            try
            {
                regKey = Registry.CurrentUser.OpenSubKey(regRamp, true);
                if (regKey == null)
                {
                    regKey = Registry.CurrentUser.CreateSubKey(regRamp);
                }

                regKey.SetValue("RampWindowsServiceHostName", Settings.ServiceHostName, RegistryValueKind.String);
                regKey.SetValue("RampWindowsServicePortNumber", Settings.ServicePortNumber);

                //Debug.WriteLine("Ramper AddIn: Settings: Assigning RampWindowsServiceHostName: " + Settings.ServiceHostName);
                //Debug.WriteLine("Ramper AddIn: Settings: Assigning RampWindowsServicePortNumber: " + Settings.ServicePortNumber);
            }
            catch (Exception exception)
            {
                Debug.WriteLine("Ramper AddIn: Ramp Service failed to save: " + exception.Message);
            }
            finally
            {
                if (regKey != null)
                {
                    regKey.Close();
                }
            }
        }
        #endregion Public Methods
    }
}
